//
//  RegisterViewController.swift
//  Gaoloumi
//
//  Created by Jasper on 3/30/16.
//  Copyright © 2016 Jasper. All rights reserved.
//

import UIKit
import SVProgressHUD
import AVOSCloud

class RegisterViewController: RootViewController,UITextFieldDelegate {

    @IBOutlet weak var nameTextFiels: UITextField!
    @IBOutlet weak var emailTextField: UITextField!
    @IBOutlet weak var pass1TextField: UITextField!
    @IBOutlet weak var pass2TextField: UITextField!
    
    @IBOutlet weak var registerButton: UIButton!
    
    @IBOutlet weak var height1: NSLayoutConstraint!
    @IBOutlet weak var height2: NSLayoutConstraint!
    @IBOutlet weak var height3: NSLayoutConstraint!
    @IBOutlet weak var height4: NSLayoutConstraint!
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.title = "注册"
        
        height1.constant = 0.5
        height2.constant = 0.5
        height3.constant = 0.5
        height4.constant = 0.5
        
        registerButton.layer.cornerRadius = 3
        registerButton.layer.masksToBounds = true
        registerButton.setBackgroundImage(UIImage.init(color: UIColor.mainColor()), for: UIControlState())
    }
    
    @IBAction func rigisterButtonCliked(_ sender: UIButton) {
        rigister()
    }

    func rigister() {
        if nameTextFiels.text == nil || nameTextFiels.text!.isEmpty {
            SVProgressHUD.showError(withStatus: "请输入用户名")
            return
        } else  if emailTextField.text == nil || emailTextField.text!.isEmpty {
            SVProgressHUD.showError(withStatus: "请输入Email")
            return
        } else  if pass1TextField.text == nil || pass1TextField.text!.isEmpty {
            SVProgressHUD.showError(withStatus: "请输入密码")
            return
        } else  if pass2TextField.text == nil || pass2TextField.text!.isEmpty {
            SVProgressHUD.showError(withStatus: "请输入确认密码")
            return
        }
        if pass1TextField.text != pass2TextField.text {
            SVProgressHUD.showError(withStatus: "两次输入的密码不一致")
            return
        }
        
        if !emailTextField.text!.isEmail() {
            SVProgressHUD.showError(withStatus: "请输入正确的邮箱地址")
            return
        }
        
        let user = AVUser()
        user.username = nameTextFiels.text
        user.email = emailTextField.text
        user.password = pass1TextField.text
        user.setObject(0, forKey: kUserKeyReplyCount)
        user.setObject(0, forKey: kUserKeySendCount)
        
        SVProgressHUD.show(withStatus: "数据提交中...")
        user.signUpInBackground { (succeeded: Bool, error: Error?) in
            if succeeded {
                // 成功
                SVProgressHUD.showSuccess(withStatus: "注册成功")
                self.navigationController?.popToRootViewController(animated: true)
            } else {
                SVProgressHUD.showError(withStatus: error?.localizedDescription)
            }
        }
    }
    
    @IBAction func tapGesture(_ sender: UITapGestureRecognizer) {
        for subView in view.subviews {
            if let sub = subView as? UITextField {
                sub.resignFirstResponder()
            }
        }
    }
    
    
    func textFieldShouldReturn(_ textField: UITextField) -> Bool {
        
        if textField.tag <= 2 {
            let text = view.viewWithTag(textField.tag+1) as? UITextField
            text?.becomeFirstResponder()
        } else {
            rigister()
        }
        
        return true
    }
    

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

}
